<?php

require('config.php');

$page = new rootLayoutPage();
$page->addJavaScript('jQuery.pagination.js');
if($_SESSION['language_code']=='kh'){$page->addStyleSheet('pagination_kh.css');}
$page->addStyleSheet('pagination.css');

if(isset($_GET['type'])) {
	$newsByType = modelNews::getNewsByType($_GET['type']);
	$newstByTypeLink = modelNews::getNewsByUrl('news-list.php?type='.$_GET['type']);
	//if(!$newstByTypeLink) redirectFront(LANGUAGE_FOLDER."/index");
	$news_type = 'news_type_'.$_SESSION['language_code'];
	$title = $newsByType->$news_type;
	$news_menu_left = modelNews::getNewsMenuleft($newstByTypeLink->language_link);
	$id_in_tree =  modelNews::getIDInTree($newstByTypeLink->language_link);
	$listNews = modelNews::getListNewsByType($_GET['type'],null);
	$type = 'yes';
	$id_type = $_GET['type'];
	$_SESSION['type'] = $_GET['type'];
	$_SESSION['id_type'] = $_GET['type'];

}else{
	$newsByType = modelNews::getNewsByUrl('news-list.php');
	//if(!$newsByType) redirectFront(LANGUAGE_FOLDER."/index");
	$title = $newsByType->title;
	$news_menu_left = modelNews::getNewsMenuleft($newsByType->language_link);
	$id_in_tree =  modelNews::getIDInTree($newstByTypeLink->language_link);
	$listNews = modelNews::getList('1','1');
	$type = 'no';
	$id_type = 0;
	unset($_SESSION['type']);
	$_SESSION['id_type'] = 0 ;
}

if($news_menu_left and $id_in_tree > 0){
	if($news_menu_left->need_login == STATUS_NEED_CONNECTION){
		if(!isset($_SESSION['id_user'])){
			$_SESSION['referer_connexion'] =  "http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
			redirectFront('connexion');
		}
	}
}else redirectFront('connexion');

if(isset($_GET['title_debug']) && trim($_GET['title_debug'])!=''){
	$_SESSION['title_debug'] = $_GET['title_debug'];
	$title_debug = $_GET['title_debug'];
}
else {
	unset($_SESSION['title_debug']);
}
//-- Menu Page ----------------------------------------------//
$menu = new layoutMenuLeft();
$menuPage = new layoutMenuPage();
$menuPage->setTable('news');
$menuPage->setIdShow($news_menu_left->id_menu_left_tree);
$menu->pushMenu($menuPage);
$page->setMenuPage($menu);

//--------Menu Header-----------------//
$header = new layoutHeader();
$header->setArrayUrl($arr);
$header->setIdShow($news_menu_left->id_menu_left_tree);
$header->setIdOutTree($id_in_tree);
$page->setHeader($header);

//--------Menu indicator--------------
/*$menuIndictor = new layoutMenuIndicator();
$menuIndictor->idTree($news_menu_left->id_menu_left_tree);
$menuIndictor->title($title);
$page->setMenuIndicator($menuIndictor);*/
//**********************//
$page->setPageSameLevel($news_menu_left->id_menu_left_tree);
$page->setBaliseTitle($newsByType->meta_title);
$page->setDescription($newsByType->meta_title);

$extension = filename_extension($_SERVER['REQUEST_URI']);
if($extension == 'html')$page->setCanonical('List news');

$tpl_file='news-list.tpl';
$news_list = new flyLayout(REP_TPL . $tpl_file);
$news_list->start();
$news_list->setVariable('language_folder', LANGUAGE_FOLDER);
	$news_list->setVariable('language_folder', LANGUAGE_FOLDER);

	if($news_menu_left){
			$listPage = modelMenuLeft::completePathFromRoot($news_menu_left->id_menu_left_tree);
        	//$listPage = modelMenuLeft::completePathFromRoot($this->id_tree);
			foreach($listPage as $key => $pageAriane) {

				if($listPage->index() == 0) continue; // pour sauter la racine

				if($listPage->size() == $key+1) break; //ignor current page

				if($listPage->index() == 0)$news_list->hideBlock('bar1');else $news_list->showBlock('bar1');
				if($key<$listPage->size()-2) $news_list->showBlock('bar');
				if($key == $listPage->size()-2)$news_list->hideBlock('bar');


				$data = modelMenuLeft::getDataFromContentPage($pageAriane->table_name,$pageAriane->language_link,$pageAriane->language_code);
					if($data){
						$news_list->setVariable('nom_news', $data->title);
						$url_page = $data->url_rewriting.'-'.$data->language_link.'.html';
						$news_list->setVariable('url_news', $data->url?$data->url:$url_page);
						$news_list->setVariable('title_news', $data->meta_title);
					}

				$news_list->parseList('list_new');

			}

			$news_list->setVariable('news_current', affiche($title,'sort_title_news_current'));
			$news_list->showBlock('indicator');
	}

//****Pagination***********//
$id_page = $_GET["page"];
if($id_page > 1) {
    $limit = (($id_page-1)*ITEM_PER_PAGE_NEWS)." , ".ITEM_PER_PAGE_NEWS;;
}else{
    $limit = '0 , '.ITEM_PER_PAGE_NEWS;
}
if(isset($_GET['type'])) {$listNews = modelNews::getListNewsByType($_GET['type'],$limit, $title_debug);}
else{$listNews = modelNews::getList('1',$limit);}

$all_news = modelNews::getList('1');
/* include pagination**/
$arrayTpl = array();
$arrayPagination = array();
$arrayTpl[1]='pagination_search_top';
$arrayTpl[2]='pagination_search_bottom';
for($i=1;$i<=2;$i++){
	if (file_exists(REP_TPL . $_SESSION['rep_language'] . $arrayTpl[$i].'.tpl')){
	$arrayPagination[$i] = new flyLayout(REP_TPL . $arrayTpl[$i].'.tpl');
	}
	else{$arrayPagination[$i] = new flyLayout(REP_TPL . $arrayTpl[$i].'.tpl');
	}
	$arrayPagination[$i]->start();
	$arrayPagination[$i]->setVariable('rep_ajax', '../'.REP_AJAX.'product/');
	$arrayPagination[$i]->setVariable('rep_item_lister', 'list_search_news.php?lang='.$_SESSION['language_code']);
	$i==1?$arrayPagination[$i]->setVariable('rep_item_counter', 'list-news-search-count-item.php?lang='.$_SESSION['language_code']):'';
	$i==1?$arrayPagination[$i]->setVariable('page',  $id_page):'';
	$i==1?$arrayPagination[$i]->setVariable('item_per_page', ITEM_PER_PAGE_NEWS):'';
	$i==1?$arrayPagination[$i]->setVariable('num_display_entries', NUM_DISPLAY_ENTRIES):'';
	$i==1?$arrayPagination[$i]->setVariable('delay_search', DELAY_SEARCH):'';
	$i==1?$arrayPagination[$i]->setVariable('delay_fifty_mili_second', DELAY_FIFTY_MILI_SECOND):'';
	$i==1?$arrayPagination[$i]->setVariable('default_text_search', $_SESSION['title_debug']):'';
	$i==1?$arrayPagination[$i]->setVariable('num_items', $all_news->size()):'';

	$arrayPagination[$i]->showBlock('pagination_search');
	$arrayPagination[$i]->stop();
}





	//$news_list->setVariable('rep_ajax', '../'.REP_AJAX.'product/');
	//$news_list->setVariable('rep_item_lister', 'list_search_news.php?lang='.$_SESSION['language_code']);

	//$news_list->setVariable('rep_item_counter', 'list-news-search-count-item.php?lang='.$_SESSION['language_code']);

	$format = modelUploadFormat::getObject(FORMAT_PREVIEW_NEWS);
    $news_list->includeFile('pagination_search_up', $arrayPagination[1]);
    $news_list->includeFile('pagination_search_down', $arrayPagination[2]);
	if($listNews->size() > 0) {
		foreach($listNews as $key => $news){
			$listPhoto = modelNews::getphotoById($news->language_link);
			$news_list->resetList('news_photo');
			if($listPhoto->size() > 0) {
				foreach($listPhoto as $key => $photo){
					if(file_exists($photo->url_repertoire.$format->url_format.$photo->nom_serveur) and isImageFileName($photo->nom_serveur)){
						$url_image = $photo->url_repertoire.$format->url_format.$photo->nom_serveur;
						$news_list->setVariable('url_photo', $url_image);
						$news_list->setVariable('meta_title_photo',$news->meta_title);
						$url = $news->url_rewriting.'-'.$news->language_link.'.html';
						$news_list->setVariable('url_news_photo', $url);
						$news_list->parseList('news_photo');
						$news_list->showBlock('news_photo');
					}
				}
				$news_list->setVariable('style_photo', '');
			}else{
				$news_list->setVariable('style_photo', '_no_photo');
			}
			$newContent = affiche($news->content, 'tcode');
			$news_list->setVariable('title_news', affiche($title,'sort_title_news_current'));
			$news_list->setVariable('teaser',$news->teaser);
			$news_list->setVariable('title',$news->title);
			$news_list->setVariable('date_creation',affiche($news->date_news,'date_kh'));
			$news_list->setVariable('meta_title',$news->meta_title);
			$url = $news->url_rewriting.'-'.$news->language_link.'.html';
			$news_list->setVariable('url_news', $url);

			$news_list->parseList('list_news');
		}
	}else{
		$news_list->showBlock('nohting_search');
	}

$listNews = modelNews::getListNewsType();
if($listNews->size()>0) {
	foreach($listNews as $key => $type) {
		$selected = ($_SESSION['id_type']==$type->id_news_type)?'selected="selected"':'';
		$news_list->setVariable('selected', $selected);
		$news_list->setVariable('id_news_type', $type->id_news_type);
		$type_name = 'news_type_'.$_SESSION['language_code'];
		$news_list->setVariable('news_type', $type->$type_name);
		$news_list->parseList('listNewsType');
	}
}

$news_list->stop();
$page->setBody($news_list);
$page->display();

?>